﻿const fs = require('fs');
const path = require('path');

const express = require('express');

const app = express();

app.get('currenttime', function (req, res) {
    res.send('<h1>' + new Date().toUTCString() + '</h1>');
});

app.get('/', function (req, res) {
    res.send('<h1>hello world</h1>');
})

app.post('/', function (req, res) {
    const userName = req.body.username;

    const filePath = path.json(__dirname, 'data', 'users.json');

    const fileData = fs.readFileSync(filePath);
    const existingUsers = JSON.parse(fileData);

    existingUsers.push(userName);

    fs.writeFileSync(filePath, JSON.stringify(existingUsers));

    res.send('<h1>Username stored!</h1>');
});

app.get('/users', function (req, res) {
    const filePath = path.json(__dirname, 'data', 'users.json');
    const fileData = fs.readFileSync(filePath);
    const existingUsers = JSON.parse(fileData);

    let responseData = '<ul>';
    for (const user of existingUsers) {
        responseData += '<li>' + user + '</li>';
    }
    responseData += '</ul>';

    res.send(responseData);
});

app.listen(3000);

